package day14;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class XMLSearch {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        System.out.println("请输入要查找的课程名称:");
        String name=in.nextLine();
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            //XMLRead.class.getClassLoader().getResourceAsStream("day14/demo.xml");
            doc = reader.read(XMLRead.class.getClassLoader().getResourceAsStream("day14/demo.xml"));
            //doc = reader.read(new FileInputStream("java高级\\src\\day14\\demo.xml"));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        List<Element> list = doc.selectNodes("/books/book[name='" + name + "']");
        if(list.size()==0){
            System.out.println("没有找到这个名字的课程");
            return;
        }
        for(Element e:list){
            int index = Integer.parseInt(e.attributeValue("index"));
            Element ename = (Element)e.selectSingleNode("name");
            String bookname = ename.getText();
            Element diff = (Element)e.selectSingleNode("difficult");
            String difficult = diff.getText();
            Element des = (Element)e.selectSingleNode("des");
            String descrp = des.getText();
            System.out.printf("序号=%d,名称=%s,难度 %s,描述=%s\n",index,bookname,difficult,descrp);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        }
    }
}
